define(['moment'], function (moment) {
    return ['$scope', '$filter', '$stateParams', 'config', 'MMWService',
    function ($scope, $filter, $stateParams, config, $MMWService) {
        $scope.init = function(){
            $scope.condition = {
                pdline_no:'',
                op_no:'',
                position_no:'',
                material_name:'',
                isModify:false
            };
            $scope.itemList = [];
            $scope.positions = [];
            if($stateParams.data){
                $scope.condition = $stateParams.data;
                $scope.condition.material_name = '';
                $scope.condition.isModify = false;
                LoadPosition();
            }
        };
        $scope.back = function(){
            $scope.$root.hideMenu();
            $scope.$root.changeProgram('home', true);
        };
        $scope.confirm = function(){
            position_collectMTL_set_process({collectMTL_set:$scope.itemList},function(){
                LoadPositionMaterial();
            });
        };

        //生產線開窗
        $scope.loadLine = function(){
            $scope.$root.getService('E04Service', function($E04Service){
                $E04Service.loadLine($scope.condition, function(data){
                    $scope.condition = data;
                    $scope.positions = [];
                    $scope.loadOP();
                });
            });
        };
        //作業站
        $scope.loadOP = function(){
            $scope.$root.getService('E04Service', function($E04Service){
                $E04Service.loadOP($scope.condition, function(data){
                    $scope.condition = data;
                    LoadPosition();
                });
            });
        };
        //作業站
        $scope.keyinOP = function(event){
            if(event.keyCode == 13)
                LoadPosition();
        };
        // 物料名稱開窗
        $scope.loadMaterial = function(){
            $MMWService.showQueryListPNIG({
                page_mode: 'position_collectMTL_log_get',
                pageRecord:50,
                condition: [
                    {name:$filter('translate')('common.queryList.material_name'), column_name:'MaterialName', query_mode:'2', value:$scope.condition.material_name}
                ],
                header: [
                    {label:$filter('translate')('common.queryList.material_name'), code:'materialname'}
                ],
                setCondition:function(conditions){
                    conditions.push({column_name:'PDLineNo', query_mode:'0', merge_condition_model:'AND', value:$scope.condition.pdline_no });
                    conditions.push({column_name:'OPNo', query_mode:'0', merge_condition_model:'AND', value:$scope.condition.op_no });
                    conditions.push({column_name:'PositionNo', query_mode:'0', merge_condition_model:'AND', value:$scope.condition.position_no });
                    return conditions;
                },
                selectCode: $scope.condition.material_name||'',
                confirm : function(selectItem){
                    $scope.condition.material_name = selectItem.materialname;
                    $scope.AddMaterial();
                }
            });
        };
        //新增物料名稱
        $scope.AddMaterial = function(event){
            if(event == undefined || event.keyCode == 13){
                var _msg = ValidatePositionInfo($scope.condition);
                if(_msg != ''){
                    $scope.$root.showAlert(_msg);
                    return;
                }

                _msg = ValidateMatirial($scope.condition.material_name);
                if(_msg == 'deleted'){
                    return;
                } else if(_msg != ''){
                    $scope.$root.showAlert(_msg);
                    return;
                }

                $scope.itemList.push({
                    material_name:$scope.condition.material_name,
                    line_no: $scope.condition.pdline_no,
                    op_no: $scope.condition.op_no,
                    position_no: $scope.condition.position_no,
                    mode:'N'
                });
                $scope.condition.material_name = '';
            }
        }
        //移除物料名稱
        $scope.RemoveMaterial = function(item){
            if(item.mode == 'N'){
                var _index = $scope.itemList.indexOf(item);
                $scope.itemList.splice(_index, 1);
            } else
                item.mode = 'D';
        }
        //切換工位
        $scope.ChangePosition = function(item){
            var _unsave = false;
            for(var i = 0; i < $scope.itemList.length; i++){
                if($scope.itemList[i].mode == 'D'
                    || $scope.itemList[i].mode == 'N'){
                    _unsave = true;
                    break;
                }
            }
            if(_unsave){
                $scope.$root.showConfirm($filter('translate')('E05.msg.unsave_check'), function(dialog){
                    dialog.hide();
                    position_collectMTL_set_process({collectMTL_set:$scope.itemList},function(){
                        $scope.condition.position_no = item.page_no;
                        LoadPositionMaterial();
                    });
                }, function(dialog){
                    dialog.hide();
                    $scope.condition.position_no = item.page_no;
                    LoadPositionMaterial();
                });
            } else {
                $scope.condition.position_no = item.page_no;
                LoadPositionMaterial();
            }
        }
        //進入工位機
        $scope.GotoE01 = function(){
            var _msg = ValidatePositionInfo($scope.condition);
            if(_msg != ''){
                $scope.$root.showAlert(_msg);
                return;
            }

            var _unsave = false;
            for(var i = 0; i < $scope.itemList.length; i++){
                if($scope.itemList[i].mode == 'D'
                    || $scope.itemList[i].mode == 'N'){
                    _unsave = true;
                    break;
                }
            }
            if(_unsave){
                $scope.$root.showConfirm($filter('translate')('E05.msg.unsave_check'), function(dialog){
                    dialog.hide();
                    position_collectMTL_set_process({collectMTL_set:$scope.itemList},function(){
                        gotoE01();
                    });
                }, function(dialog){
                    dialog.hide();
                    gotoE01();
                });
            } else {
                gotoE01();
            }
        }

        // 檢核工位資訊是否輸入完整
        function ValidatePositionInfo(data){
            var msg = '';
            if(data.pdline_no == ''){
                msg += $filter('translate')('E01.msg.line_no_empty')+'<br>';
            }
            if(data.op_no == ''){
                msg += $filter('translate')('E01.msg.op_no_empty')+'<br>';
            }
            if(data.position_no == ''){
                msg += $filter('translate')('E01.msg.position_no_empty')+'<br>';
            }
            return msg;
        }

        function ValidateMatirial(material_name){
            if(material_name == ''){
                //物料名稱不可空白
                return $filter('translate')('E05.msg.material_name_empty');
            }
            for(var i = 0; i < $scope.itemList.length; i++){
                if($scope.itemList[i].material_name == material_name){
                    if($scope.itemList[i].mode == 'D'){
                        $scope.itemList[i].mode = undefined;
                        return 'deleted';
                    } else {
                        //已經輸入過物料名稱'+material_name
                        return $filter('translate')('E05.msg.material_name_duplicate')+material_name;
                    }
                }
            }
            return '';
        }

        function LoadPosition(){
            $MMWService.pageNumberInfoGet({
                page_mode : '28',
                query_condition_info:[
                    {column_name:'PDLineNo', query_mode:'0', merge_condition_model:'AND', value:$scope.condition.pdline_no },
                    {column_name:'OPNo', query_mode:'0', merge_condition_model:'AND', value:$scope.condition.op_no }
                ]
            }, function(itemList){
                $scope.positions = itemList;
                var _result = '';
                for(var i = 0; i < $scope.positions.length; i++){
                    if($scope.positions[i].page_no == $scope.condition.position_no){
                        _result = $scope.positions[i].page_no;
                        break;
                    }
                }
                if(_result != ''){
                    $scope.condition.position_no = _result;
                    LoadPositionMaterial();
                } else if($scope.positions && $scope.positions.length > 0){
                    $scope.condition.position_no = $scope.positions[0].page_no;
                    LoadPositionMaterial();
                } else {
                    $scope.condition.position_no = '';
                }
            });
        }

        function LoadPositionMaterial(){
            $MMWService.pageNumberInfoGet({
                page_mode : 'position_collectMTL_set_get',
                query_condition_info:[
                    {column_name:'PDLineNo', query_mode:'0', merge_condition_model:'AND', value:$scope.condition.pdline_no },
                    {column_name:'OPNo', query_mode:'0', merge_condition_model:'AND', value:$scope.condition.op_no },
                    {column_name:'PositionNo', query_mode:'0', merge_condition_model:'AND', value:$scope.condition.position_no }
                ]
            }, function(itemList){
                $scope.itemList = itemList;
            });
        }

        function position_collectMTL_set_process(content, callback){
            $MMWService.sendToServer({
                uri: 'ServicesSTD.Module_SN.position_collectMTL_set_process',
                content: content,
                success: function (data) {
                    $scope.$root.showToast($filter('translate')('common.msg.success'));
                    if(typeof(callback) == 'function')
                        callback();
                }
            });
        }

        function gotoE01(){
            $scope.$root.hideMenu();
            $scope.$root.changeProgram('E01', {data:{
                pdline_no:$scope.condition.pdline_no,
                op_no:$scope.condition.op_no,
                position_no:$scope.condition.position_no,
                single_confirm:$scope.condition.single_confirm
            }});
        }
    }];
});
